package homework3;

import java.time.LocalTime;
import java.util.ArrayList;
import java.util.Scanner;

public class Test {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        //创建三个对象
        Commodity commodity1 = new Commodity("鸭蛋", 10);
        Commodity commodity2 = new Commodity("摇头风扇", 2);
        Commodity commodity3 = new Commodity("瓢虫项链", 5);
        //创建一个集合
        ArrayList<Commodity> commodities = new ArrayList<>();
        commodities.add(commodity1);
        commodities.add(commodity2);
        commodities.add(commodity3);

        // 假设抢购时间范围是每天的8点到10点
        LocalTime startTime = LocalTime.of(8, 0);  // 抢购开始时间
        LocalTime endTime = LocalTime.of(10, 0);  // 抢购结束时间
        // 获取当前日期和时间
        LocalTime currentTime = LocalTime.now();

        if (currentTime.isAfter(startTime) && currentTime.isBefore(endTime)) {

            System.out.println("秒杀开始");
            while (true) {
                System.out.println("请输入你要购买的商品和数量");
                String shop = scanner.nextLine();
                if (shop.contains("鸭蛋") || shop.contains("风扇") || shop.contains("项链")) {
                    while (true) {
                        System.out.println("请选择你购买商品数量");
                        int nums = scanner.nextInt();
                        if (commodity1.getNum() >= nums || commodity2.getNum() >= nums || commodity3.getNum() >= nums) {
                            //库存充足,请5
                            System.out.println("我购买了商品" + shop + ", 共买了" + nums + "个");
                            break;
                        } else {
                            System.out.println("库存不足,请你重新选择数量");
                        }
                    }
                    break;
                } else {
                    System.out.println("您选择的商品不存在,请重新选择");
                }
            }
        } else {
            System.out.println("时间超时,不能够选购");
        }
    }
}
